package com.example.mall.common.core.configuration;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.List;

@Configuration
@EnableOpenApi
@EnableKnife4j
public class SwaggerConfiguration {
    @Value("${swagger.enable}")
    private boolean enabled;
    @Value("${swagger.title}")
    private String title;
    @Value("${swagger.description}")
    private String description;
    @Value("${swagger.version}")
    private String version;
    @Value("${swagger.api-package}")
    private String apiPackage;

    @Bean
    public Docket createDocket() {
        return new Docket(DocumentationType.OAS_30)
                .groupName("全部接口")
                .enable(enabled)
                .apiInfo(getApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(apiPackage))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket createDocket1() {
        return new Docket(DocumentationType.OAS_30)
                .groupName("前台接口v1")
                .enable(enabled)
                .apiInfo(getApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(apiPackage+".v1"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket createDocket2() {
        return new Docket(DocumentationType.OAS_30)
                .groupName("前台接口v2")
                .enable(enabled)
                .apiInfo(getApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(apiPackage+".v2"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket createDocket3() {
        return new Docket(DocumentationType.OAS_30)
                .groupName("后台接口 cms")
                .enable(enabled)
                .apiInfo(getApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(apiPackage+".cms"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo getApiInfo() {
        return new ApiInfoBuilder()
            .title(title)
            .description(description)
            .version(version)
            .build();
    }
}
